/*
 * MatrixView.java
 */
package matrix;

import Jama.Matrix;
import Jama.SingularValueDecomposition;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * The application's main frame.
 */
public class MatrixView extends FrameView {

    MatrixCalculator calculator;

    public MatrixView(SingleFrameApplication app) {
        super(app);

        initComponents();

        calculator = new MatrixCalculator();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String) (evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer) (evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = MatrixApp.getApplication().getMainFrame();
            aboutBox = new MatrixAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        MatrixApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        enterButton = new javax.swing.JButton();
        createButton = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jTextField6 = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        list1 = new java.awt.List();
        jButton1 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        stepsButton = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jLabel11 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(matrix.MatrixApp.class).getContext().getResourceMap(MatrixView.class);
        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jTextField2.setText(resourceMap.getString("jTextField2.text")); // NOI18N
        jTextField2.setName("jTextField2"); // NOI18N

        jTextField3.setText(resourceMap.getString("jTextField3.text")); // NOI18N
        jTextField3.setName("jTextField3"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        enterButton.setText(resourceMap.getString("enterButton.text")); // NOI18N
        enterButton.setName("enterButton"); // NOI18N
        enterButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                enterButtonActionPerformed(evt);
            }
        });

        createButton.setText(resourceMap.getString("createButton.text")); // NOI18N
        createButton.setName("createButton"); // NOI18N
        createButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createButtonActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] {"Determinate", "Inverse","Linear Equation","Multiplication","Transpose" ,"Eigen Values","Newton Iteration","Schur Factorization","Diagonalization","Singular Value Decomposition" ,"Dot Product"}));
        jComboBox1.setName("jComboBox1"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jTextField4.setName("jTextField4"); // NOI18N

        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jButton5.setText(resourceMap.getString("jButton5.text")); // NOI18N
        jButton5.setName("jButton5"); // NOI18N
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jTextField5.setText(resourceMap.getString("jTextField5.text")); // NOI18N
        jTextField5.setName("jTextField5"); // NOI18N

        jTextField6.setName("jTextField6"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane2.setViewportView(jTextArea1);

        list1.setMultipleMode(true);
        list1.setName("list1"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel9.setName("jLabel9"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator2.setName("jSeparator2"); // NOI18N

        stepsButton.setText(resourceMap.getString("stepsButton.text")); // NOI18N
        stepsButton.setName("stepsButton"); // NOI18N
        stepsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stepsButtonActionPerformed(evt);
            }
        });

        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextArea2.setColumns(20);
        jTextArea2.setRows(5);
        jTextArea2.setName("jTextArea2"); // NOI18N
        jScrollPane1.setViewportView(jTextArea2);

        jLabel11.setText(resourceMap.getString("jLabel11.text")); // NOI18N
        jLabel11.setMaximumSize(new java.awt.Dimension(0, 23));
        jLabel11.setMinimumSize(new java.awt.Dimension(0, 12));
        jLabel11.setName("jLabel11"); // NOI18N

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout mainPanelLayout = new org.jdesktop.layout.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(mainPanelLayout.createSequentialGroup()
                        .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, mainPanelLayout.createSequentialGroup()
                                .add(jLabel5)
                                .add(18, 18, 18)
                                .add(jTextField4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(18, 18, 18)
                                .add(createButton)
                                .add(111, 111, 111))
                            .add(mainPanelLayout.createSequentialGroup()
                                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel2)
                                    .add(mainPanelLayout.createSequentialGroup()
                                        .add(jLabel7)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(jTextField5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 33, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(mainPanelLayout.createSequentialGroup()
                                        .add(jLabel8)
                                        .add(18, 18, 18)
                                        .add(jTextField6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 33, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .add(43, 43, 43)
                                        .add(jLabel1)
                                        .add(14, 14, 14)
                                        .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 31, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 18, Short.MAX_VALUE)
                                        .add(enterButton)
                                        .add(9, 9, 9))
                                    .add(mainPanelLayout.createSequentialGroup()
                                        .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 32, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                        .add(jLabel3)
                                        .add(9, 9, 9)
                                        .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 37, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 209, Short.MAX_VALUE))
                                    .add(jLabel4)))
                            .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 204, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(mainPanelLayout.createSequentialGroup()
                                    .add(jButton4)
                                    .add(59, 59, 59)
                                    .add(stepsButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 80, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 38, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(mainPanelLayout.createSequentialGroup()
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(list1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 133, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(mainPanelLayout.createSequentialGroup()
                                .add(28, 28, 28)
                                .add(jLabel6))
                            .add(mainPanelLayout.createSequentialGroup()
                                .add(18, 18, 18)
                                .add(jButton5)))
                        .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(mainPanelLayout.createSequentialGroup()
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 257, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(mainPanelLayout.createSequentialGroup()
                                .add(94, 94, 94)
                                .add(jLabel11, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(mainPanelLayout.createSequentialGroup()
                                .add(104, 104, 104)
                                .add(jButton1)))
                        .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(mainPanelLayout.createSequentialGroup()
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(mainPanelLayout.createSequentialGroup()
                                .add(84, 84, 84)
                                .add(jLabel10))
                            .add(mainPanelLayout.createSequentialGroup()
                                .add(105, 105, 105)
                                .add(jButton2))))
                    .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 955, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(410, Short.MAX_VALUE))
            .add(mainPanelLayout.createSequentialGroup()
                .add(398, 398, 398)
                .add(jLabel9)
                .addContainerGap(1137, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainPanelLayout.createSequentialGroup()
                .add(20, 20, 20)
                .add(jLabel9)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 365, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(mainPanelLayout.createSequentialGroup()
                        .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(mainPanelLayout.createSequentialGroup()
                                .add(5, 5, 5)
                                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                    .add(jLabel2)
                                    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(jLabel3)))
                            .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                .add(jLabel6)
                                .add(jLabel11, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(jLabel10)))
                        .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(mainPanelLayout.createSequentialGroup()
                                .add(30, 30, 30)
                                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                    .add(jLabel5)
                                    .add(jTextField4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(createButton))
                                .add(42, 42, 42)
                                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                    .add(jLabel7)
                                    .add(jLabel8)
                                    .add(jTextField5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(jTextField6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(jLabel1)
                                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(enterButton))
                                .add(47, 47, 47)
                                .add(jLabel4)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(18, 18, 18)
                                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                    .add(jButton4)
                                    .add(stepsButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 33, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, mainPanelLayout.createSequentialGroup()
                                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(list1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
                                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
                                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                        .add(jButton5)
                                        .add(jButton1))
                                    .add(jButton2))
                                .add(7, 7, 7)))))
                .add(316, 316, 316))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(matrix.MatrixApp.class).getContext().getActionMap(MatrixView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        org.jdesktop.layout.GroupLayout statusPanelLayout = new org.jdesktop.layout.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(statusPanelLayout.createSequentialGroup()
                .add(767, 767, 767)
                .add(statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(statusPanelSeparator, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 768, Short.MAX_VALUE)
                    .add(statusPanelLayout.createSequentialGroup()
                        .add(statusMessageLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 592, Short.MAX_VALUE)
                        .add(progressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(statusAnimationLabel)
                        .addContainerGap())))
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(statusPanelLayout.createSequentialGroup()
                .add(statusPanelSeparator, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 38, Short.MAX_VALUE)
                .add(statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(statusMessageLabel)
                    .add(statusAnimationLabel)
                    .add(progressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
    String matrixName = list1.getSelectedItem();
    Matrix m = null;

    try {
        m = calculator.getMatrix(matrixName);
        calculator.printOut(matrixName, m, m.getColumnDimension(), jTextArea1);
    } catch (NullPointerException e) {
        JOptionPane.showMessageDialog(null, "Please select a matrix in the database.");
    }

}//GEN-LAST:event_jButton5ActionPerformed

private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_enterButtonActionPerformed
   try{

    String Selection = list1.getSelectedItem() + "";

        Double tempDbl = Double.parseDouble(jTextField1.getText());
        Matrix m = null;
        String matrixName = list1.getSelectedItem();

        m = calculator.getMatrix(matrixName);
        if(Integer.parseInt(jTextField5.getText()) <=  m.getRowDimension() && Integer.parseInt(jTextField6.getText()) <= m.getColumnDimension()){
           if(Integer.parseInt(jTextField5.getText()) >=  1 && Integer.parseInt(jTextField6.getText()) >= 1){

            calculator.InputData(Integer.parseInt(jTextField5.getText()),Integer.parseInt(jTextField6.getText()),tempDbl,Selection);
            calculator.printOut(matrixName,m,m.getColumnDimension(),jTextArea1);
            
            }
        }
        else{
            JOptionPane.showMessageDialog(null, "Please enter the appropriate row and column positions");
        }
   }
   catch(NullPointerException e){
             JOptionPane.showMessageDialog(null, "Please select a matrix in the database.");
    }
    catch(NumberFormatException e){
        JOptionPane.showMessageDialog(null, "Please create a matrix in the database and input values for row and column first.");
    }
    
    jTextField5.setText("");
    jTextField6.setText("");
    jTextField1.setText("");
}//GEN-LAST:event_enterButtonActionPerformed

private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createButtonActionPerformed
    int row = 0,col = 0;
    String name = null;
    try{
    row = Integer.parseInt(jTextField2.getText());
    col = Integer.parseInt(jTextField3.getText());
    name = jTextField4.getText()+ "  " + jTextField2.getText() + "x" + jTextField3.getText();
   
    calculator.CreateNewMatrix(row, col, name);
    list1.add(name); 
    jTextField2.setText("");
    jTextField3.setText("");
    jTextField4.setText("");}

    catch(NumberFormatException e){
     JOptionPane.showMessageDialog(null, "Matrix dimensions must be positive integers.");
}

}//GEN-LAST:event_createButtonActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
    String matrixName = list1.getSelectedItem();
    Matrix m = null;
    String function_name = (String) jComboBox1.getSelectedItem();


    if (function_name.matches("Eigen Values")) {
        try {
            m = calculator.getMatrix(matrixName);
            double[] answer = calculator.EigenValue(m);
            calculator.printOut(("Eigen Values for Matrix: " + matrixName + "\n"),jTextArea1);
            for (double d : answer) {
                calculator.printOut((Double.toString(d)),jTextArea1);
              
            }
        } catch (NullPointerException e) {
            JOptionPane.showMessageDialog(null, "Please select a matrix in the database.");
        }
    }//end if

    if(function_name.matches("Transpose")){

       if (list1.getSelectedObjects().length == 1){
          try{
            m=calculator.getMatrix(matrixName);
            Matrix transpose=calculator.Transpose(m);
            calculator.printOut("Transpose", transpose,transpose.getColumnDimension(), jTextArea1);
            }
           catch(Exception e){
            JOptionPane.showMessageDialog(null, "Exception on Transpose.");
            }
       }
       else{
           JOptionPane.showMessageDialog(null, "Select One Matrix");

       }
    }//end if

    if(function_name.matches("Inverse")){

      if (list1.getSelectedObjects().length == 1){
        try{
             m=calculator.getMatrix(matrixName);
            Matrix inverse=calculator.Inverse(m);
            calculator.printOut("Inverse", inverse,inverse.getColumnDimension(), jTextArea1);
        }
        catch(Exception e){

            if(m.getRowDimension()!= m.getColumnDimension()){
             JOptionPane.showMessageDialog(null, "Make sure your matrix is a nxn matrix");
            }
             else{
                JOptionPane.showMessageDialog(null, "The Matrix is not invertible");

             }
        }
      }
      else{
          JOptionPane.showMessageDialog(null, "Select One Matrix");
      }
    }//end if

         if(function_name.matches("Schur Factorization")){
        try{
             m=calculator.getMatrix(matrixName);
             String prompt="Enter an Eigen value of Matrix: "+matrixName;
             double eigVal=Double.parseDouble(JOptionPane.showInputDialog(null,prompt));
            SchurFactorization s=calculator.Schur(m,eigVal);
            
            calculator.printOut(("Schur Factorization of Matrix: "+matrixName+"\n"), jTextArea1);
            calculator.printOut(("With Eigen Value: "+eigVal+"\n"), jTextArea1);
            calculator.printOut("Matrix B: ", s.getB(), s.getB().getColumnDimension(), jTextArea1);
            calculator.printOut("Matrix U: ", s.getU(), s.getU().getColumnDimension(), jTextArea1);
            calculator.printOut("Matrix T: ", s.getT(), s.getT().getColumnDimension(), jTextArea1);
        }
        catch(Exception e){
             JOptionPane.showMessageDialog(null, "Exception on Schur Factorization.");
        }
         }//end if


   

    if(function_name.matches("Newton Iteration")){

    try{
       
         String[] list=list1.getSelectedItems();
         Matrix a=calculator.getMatrix(list[0]);
         Matrix b=calculator.getMatrix(list[1]);
         Matrix c=calculator.getMatrix(list[2]);

         Matrix ans=calculator.NewtonIteration(a, b, c);
         calculator.printOut("Newton Interation: ", ans, ans.getColumnDimension(), jTextArea1);
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, "Please Select at least 3 matrices(Matrix a ,b (invertible & nxn),c (nxm)) for the Newton Iteration.");
    }
   }//end if

   if(function_name.matches("Dot Product")){

     if(list1.getSelectedObjects().length == 2){
      // try{
          String[] list = list1.getSelectedItems();
          Matrix a = calculator.getMatrix(list[0]);
          Matrix b = calculator.getMatrix(list[1]);

          Matrix dotProduct = calculator.DotProduct(a, b);
          calculator.printOut("Dot Product: ",dotProduct, dotProduct.getColumnDimension(), jTextArea1);
      // }
       /*catch(Exception e){
        JOptionPane.showMessageDialog(null, "Dot Product Exception");

       }*/
     }
      else{
        JOptionPane.showMessageDialog(null, "Please select 2 Matrices");
      }

   }

    if(function_name.matches("Determinate")){

      if(list1.getSelectedObjects().length == 1){
        try{
            Matrix a = calculator.getMatrix(list1.getSelectedItem());
            double determinate = a.det();
            calculator.printOut( Double.toString(determinate), jTextArea1);
        }
        catch(Exception e){
              JOptionPane.showMessageDialog(null, "Determinant Exception");

            }
        }
      else{
          JOptionPane.showMessageDialog(null, "Select One Matrix");

      }
    }

    if(function_name.matches("Multiplication")){
        if (list1.getSelectedObjects().length == 2){
           try{

            String [] selectedList = list1.getSelectedItems();
            Matrix a = calculator.getMatrix(selectedList[0]);
            Matrix b = calculator.getMatrix(selectedList[1]);

            Matrix n = calculator.Multiply(a, b);
            String multiplyMatrixName = "Multiplied Matrix";

            calculator.printOut(multiplyMatrixName, n, n.getColumnDimension(), jTextArea1);
           }
           catch(Exception e){
               JOptionPane.showMessageDialog(null, "Multiplication Exception");
           }

            //calculator.getMatrix( );
        }
        else{
           JOptionPane.showMessageDialog(null, "Select 2 Matrices");
        }


    }

    if(function_name.matches("Linear Equation")){
        if (list1.getSelectedObjects().length == 2){

            try{
             String [] selectedList = list1.getSelectedItems();
             Matrix a = calculator.getMatrix(selectedList[0]);
             Matrix b = calculator.getMatrix(selectedList[1]);

             //if(a.getRowDimension() == b.getColumnDimension()){
                 Matrix x = calculator.LinearEquation(a, b);
                 String linearEquationName = "Linear Matrix";

                 calculator.printOut(linearEquationName, x, x.getColumnDimension(), jTextArea1);
           }

            catch(Exception e){

             JOptionPane.showMessageDialog(null, "Linear Equation Exception"); 
          }
             //else{
               //  JOptionPane.showMessageDialog(null, "Must select mxn matrix and an nx1 vector");
             //}
        }
        else{
            JOptionPane.showMessageDialog(null, "Please select 2 matrices");
        }
    }

        if(function_name.matches("Singular Value Decomposition")){
        try{
             m=calculator.getMatrix(matrixName);
            SingularValueDecomposition s=calculator.SVD(m);

            calculator.printOut(("Singular Value Decomposition of Matrix: "+matrixName+"\n"), jTextArea1);
            calculator.printOut("Matrix S: ", s.getS(), s.getS().getColumnDimension(), jTextArea1);
            calculator.printOut("Matrix U: ", s.getU(), s.getU().getColumnDimension(), jTextArea1);
            calculator.printOut("Matrix V: ", s.getV(), s.getV().getColumnDimension(), jTextArea1);
        }
        catch(Exception e){
             JOptionPane.showMessageDialog(null, "Exception on Singular Value Decomposition.");
        }
         }//end if

     if(function_name.matches("Diagonalization")){
        try{
             m=calculator.getMatrix(matrixName);
              String prompt="Enter an Eigen value of Matrix: "+matrixName;
             double eigVal=Double.parseDouble(JOptionPane.showInputDialog(null,prompt));
            SchurFactorization s=calculator.Schur(m,eigVal);

            calculator.printOut(("Diagonalization of Matrix: "+matrixName+"\n"), jTextArea1);
            calculator.printOut("Matrix U: ", s.getU(), s.getU().getColumnDimension(), jTextArea1);
            calculator.printOut("Matrix T: ", s.getT(), s.getT().getColumnDimension(), jTextArea1);
        }
        catch(Exception e){
             JOptionPane.showMessageDialog(null, "Exception on Diagonalization.");
        }
      }//end if

    
}//GEN-LAST:event_jButton4ActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    jTextArea1.setText("");
}//GEN-LAST:event_jButton1ActionPerformed

private void stepsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stepsButtonActionPerformed

    String matrixName = list1.getSelectedItem();
    Matrix m = null;
    String function_name = (String) jComboBox1.getSelectedItem();


     if(function_name.matches("Schur Factorization")){
         if (list1.getSelectedObjects().length == 1){
            try{
                 m=calculator.getMatrix(matrixName);
                String prompt="Enter an Eigen value of Matrix: "+matrixName;
                double eigVal=Double.parseDouble(JOptionPane.showInputDialog(null,prompt));
                SchurFactorization s=calculator.Schur(m,eigVal);


                 calculator.printOut(("Schur Factorization of Matrix: "+matrixName+"\n"), jTextArea2);

                 calculator.printOut("Step1:" + "\n", jTextArea2);

                calculator.printOut(("With Eigen Value: "+eigVal+"\n"), jTextArea2);

                 calculator.printOut("Step2:" + "\n", jTextArea2);

                calculator.printOut("Matrix B (A - eig vectors*Identity): ", s.getB(), s.getB().getColumnDimension(), jTextArea2);

                //Matrix r1 = s.getB().getMatrix(0, 0,0,1);
                //Matrix perp = s.getPerpendicularVector(r1);
                //calculator.printOut("Perpendicular Vector: ",perp ,perp.getColumnDimension(),  jTextArea2);

                calculator.printOut("Step3:" + "\n", jTextArea2);

                calculator.printOut("Solve for Matrix U: ", s.getU(), s.getU().getColumnDimension(), jTextArea2);

                calculator.printOut("Step4:" + "\n", jTextArea2);

                calculator.printOut("Utransposed*A*U: (Matrix T): ", s.getT(), s.getT().getColumnDimension(), jTextArea2);
        }
        catch(Exception e){
             JOptionPane.showMessageDialog(null, "Exception on Schur Factorization.");
        }
     }
   }//end if


}//GEN-LAST:event_stepsButtonActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
 jTextArea2.setText("");
}//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton createButton;
    private javax.swing.JButton enterButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private java.awt.List list1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JButton stepsButton;
    // End of variables declaration//GEN-END:variables
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private JDialog aboutBox;
}
